EFFECT.nextTrail = 0
EFFECT.timeout = 0
EFFECT.rot = 0

local matFire = Material( "effects/fire_cloud1" )
local matCore = Material( "sprites/gmdm_pickups/light" )
local matRefraction = Material( "egon_ringbeam" )
local matTripmineLaser = Material( "tripmine_laser" )

/*---------------------------------------------------------
   Initializes the effect. The data is a table of data 
   which was passed from the server.
---------------------------------------------------------*/
function EFFECT:Init( data )

	ent = data:GetEntity()
	if ( ent == NULL ) then return end
	
	--self.Normal = data:GetNormal()
	
	self.ParentEntity = ent
	self.Entity:SetPos( ent:GetPos() )
	self.Entity:SetRenderBounds( Vector()*-512, Vector()*512 )
	
	self.trails = {}
	self.trails[1] = {}
	self.trails[2] = {}
	self.trails[3] = {}
	self.trails[4] = {}
	--self.trails[5] = {}
	self.tp1 = Vector(0,0,0)
	self.tp2 = Vector(0,0,0)
	self.tp3 = Vector(0,0,0)
	self.tp4 = Vector(0,0,0)
	--self.trails[3] = {}
	self.nextTrail = 0
	self.scroll = 1
	self.rot = 0
	self.lastPos = nil
	self.tcolor = Color(255,255,255,255)
	self.ta = 255
	
	self.timeout = CurTime()+40
	self.trailout = false
	self.glowies = true
	
end


/*---------------------------------------------------------
   THINK
   Returning false makes the entity die
---------------------------------------------------------*/
function EFFECT:Think( )
	
	if(self.ParentEntity != nil and self.ParentEntity:IsValid()) then
		self.Entity:SetPos( self.ParentEntity:GetPos() )
	end
	
	--self.Entity:SetPos(self.Entity:GetPos() - Vector(0,0,1))
	if self.rot == nil then self.rot = 0 end
	self.rot = self.rot + 22
	if(self.rot > 360) then
		self.rot = 0
	end
	
	if ( self.ParentEntity == nil or !self.ParentEntity:IsValid()) then 
		self.nextTrail = CurTime()+1000
		self.glowies = false
		if(self.trailout == true) then
			return false
		end
	end
	
	if(self.timeout < CurTime()) then
		return false
	end
	
	return true
	
end

function EFFECT:Trail()
	if(self.nextTrail < CurTime()) then
		local pos = self.Entity:GetPos()
		
		if self.lastPos != nil && self.trails != nil then
			--Cuz spinning is SOO much cooler than not spinning.
			local normal = (pos - self.lastPos):Normalize()
			local angle = normal:Angle()
			angle:RotateAroundAxis(angle:Forward(),self.rot)
			local p_add = angle:Right()*12*math.sin(CurTime()*4)
			
			
			table.insert(self.trails[1],{pos = pos + p_add,alpha = self.ta,alpha2 = self.ta,cp = pos})
			self.tp1 = pos + p_add
			table.insert(self.trails[2],{pos = pos - p_add,alpha = self.ta,alpha2 = self.ta,cp = pos})
			self.tp2 = pos - p_add
			
			angle:RotateAroundAxis(angle:Forward(),90)
			p_add = angle:Right()*20*math.sin(CurTime()*4)
			table.insert(self.trails[3],{pos = pos + p_add,alpha = self.ta,alpha2 = self.ta,cp = pos})
			self.tp3 = pos + p_add
			table.insert(self.trails[4],{pos = pos - p_add,alpha = self.ta,alpha2 = self.ta,cp = pos})
			self.tp4 = pos - p_add
		end
		
		self.lastPos = pos
		self.nextTrail = CurTime()
	end
	
	render.SetMaterial( matCore )
	col = self.tcolor
	
	if(self.glowies == true) then
		render.DrawSprite( self.tp1, 20, 20, col )
		render.DrawSprite( self.tp2, 20, 20, col )
		render.DrawSprite( self.tp3, 20, 20, col )
		render.DrawSprite( self.tp4, 20, 20, col )
	end
	
	--self.scroll = self.scroll + 0
	if self.trails == nil then return end
	for l,t in pairs(self.trails) do
		if(table.getn(t) > 2) then
			for k,v in pairs(t) do
				local scroll = self.scroll
				local lv = t[k-1]
				local lv2 = t[k-2]

				if(lv != nil) then
			
					norm = (v.cp - v.pos)
					local rvec = norm*.01
					--rvec = rvec + Vector(math.random(-20,20),math.random(-20,20),math.random(-20,20))/50
					local c = col

					--render.SetMaterial( matFire )
					--HE'S GOING THE DISTANCE!
					local dist = v.pos:Distance(LocalPlayer():GetPos())
					local bsize = math.Clamp(dist/100,5,65)
					bsize = bsize * 2
					if(!v.main) then
					v.pos = v.pos - rvec
					render.SetMaterial( matTripmineLaser )
					render.StartBeam( 2 )
						render.AddBeam( v.pos, bsize, scroll + 1.5, Color( c.r, c.g, c.b, math.Clamp(v.alpha,0,255)) )
						render.AddBeam( lv.pos, bsize, scroll + 1.8, Color( c.r, c.g, c.b, math.Clamp(lv.alpha,0,255)) )	
					render.EndBeam()
					else
						if(lv2 != nil) then
							render.SetMaterial( matCore )
							render.StartBeam( 2 )
								render.AddBeam( v.pos, 25, scroll + 1.5, Color( c.r, c.g, c.b, math.Clamp(v.alpha,0,255)) )
								render.AddBeam( lv2.pos, 25, scroll + 1.51, Color( c.r, c.g, c.b, math.Clamp(lv.alpha,0,255)) )	
							render.EndBeam()
						end
					end
					
					if(v.alpha2 > 0 and self.isProton) then
					render.SetMaterial( matTripmineLaser )
					render.StartBeam( 2 )
						render.AddBeam( v.pos, 2, scroll + 1.5, Color( c.r, c.g, c.b, v.alpha2/2) )
						render.AddBeam( self.Entity:GetPos(), 2, scroll + 1.8, Color( c.r, c.g, c.b, math.Clamp(v.alpha2/2,0,255)) )			
					render.EndBeam()
					end
				end
				v.alpha = v.alpha - 6
				v.alpha2 = v.alpha2 - 10
				if(v.alpha < 0) then
					table.remove(t,k)
				end
			end
			self.trailout = false
		else
			self.trailout = true
		end
	end
end


/*---------------------------------------------------------
   Draw the effect
---------------------------------------------------------*/
function EFFECT:Render()
	self:Trail()
	
end



